{layout name="layout/header" /}
<link href="root/css/plugins/footable/footable.core.css" rel="stylesheet">
<link href="root/css/plugins/iCheck/custom.css" rel="stylesheet">

<link href="root/css/plugins/switchery/switchery.css" rel="stylesheet">
<script src="root/js/plugins/switchery/switchery.js"></script>
<!--<link href="root/css/plugins/bootstrap-table/bootstrap-table.min.css" rel="stylesheet">-->
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>优惠活动</h5>
                    <div class="ibox-tools">
                        <a class="btn btn-success btn-sm" href="{:url('add')}">新增</a>
                        <button type="button" class="btn btn-primary btn-sm refresh">刷新</button>
                    </div>
                </div>
                <div class="ibox-title">
                    <form class="form-inline">
                        <div class="form-group">
                           <input type="text" id="keyword" class="form-control" placeholder="标题" value="{$keyword}">
                        </div>
                        <a class="btn btn-success search">查找</a>
                    </form>
                </div>
                <div class="ibox-content">
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>排序</th>
                                <th>标题</th>
                                <th>状态</th>
                                <th>创建时间</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                        {volist name="data" id="vo"}
                        <tr pid="{$vo.id}" >
                            <td>{$vo.id}</td>
                            <td><input type="text" class="form-control update_text" data-field="sort" value="{$vo.sort}"></td>
                            <td>{$vo.title}</td>
                            <td>
                                <input type="checkbox" class="js-switch update_checkbox" data-field="display" {eq name="$vo.display" value="1"}checked{/eq} />
                            </td>

                            <td>{$vo.addtime|date="Y-m-d H:i",###}</td>
                            <td>
                                <!--<button type="button" class="btn btn-danger btn-sm" data-toggle="modal" href="#modal-form" onclick="change_num()">编辑权限</button>-->
                                <a type="button" class="btn btn-primary btn-sm " href="{:url('update',['id'=>$vo.id])}">编辑</a>
                                <button class="btn btn-danger btn-sm del" pid="{$vo['id']}">删除</button>

                            </td>

                        </tr>
                        {/volist}
                        </tbody>
                        <tfoot>
                        <tr>
                            <td colspan="5">
                                {$page}
                            </td>
                        </tr>
                        </tfoot>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<!--layer-->
<script src="root/js/plugins/layer/layer.min.js"></script>
<!-- iCheck -->
<script src="root/js/plugins/iCheck/icheck.min.js"></script>
<script>
    $(document).ready(function() {
        checks();
        //单选，复选按钮
        function checks () {
            $('.i-checks').iCheck({
                checkboxClass: 'icheckbox_square-green',
                radioClass: 'iradio_square-green'
            })
        };
        var elem = document.querySelectorAll('.js-switch');
        elem.forEach(function(i,index){
            var a= "a" +index;
            a = new Switchery(i, {
            color: '#1AB394'
            });
        });

        //搜索
        $('.search').click(function () {
            var keyword = $("#keyword").val().trim();
            type_id = $("#type").val();
            var url = "{:url('index')}"+'&keyword='+keyword;
            window.location.href = url;
        });
        //列表页修改
        $(".update_checkbox").change(function () {
            var field = $(this).attr('data-field');
            var value = $(this).is(':checked')?1:0;
            var id = $(this).parent().parent().attr('pid');
            update(id,field,value);
        });
        //列表页修改
        $(".update_text").blur(function () {
            var field = $(this).attr('data-field');
            var id = $(this).parent().parent().attr('pid');
            var value = $(this).val().trim();
            update(id,field,value);
        });
        function update(id,field,value) {
            $.ajax({
                url:"{:url('ajax_update')}",
                type:'post',
                dataType:'json',
                data:{field:field,value:value,id:id},
                success:function (data) {
                    console.log(data.message);
                    if(data.code == 0){
                        layer.msg(data.message);
                    }
                }
            })
        }
        //删除
        $('.del').click(function(){
            var id = $(this).attr('pid');
            var $this = $(this);

            layer.confirm('确定删掉吗？', {
                btn: ['确定','取消'] //按钮
            }, function(){
                $.ajax({
                    url:"{:url('ajax_del')}",
                    type:'post',
                    data:{id:id},
                    dataType:'json',
                    success:function (data) {
                        layer.msg(data.msg);
                        if(data.code ==1){
                            $this.parent().parent().remove();
                        }
                    }
                })
            }, function(){

            });

        });

    });
</script>
